// reduce

let tree = [
    {
        "id": 1,
        "name": "1",
        "pid": 0,
        "children": [
            {
                "id": 2,
                "name": "2",
                "pid": 1,
                "children": []
            },
            {
                "id": 3,
                "name": "3",
                "pid": 1,
                "children": [
                   {
                     "id": 4,
                     "name": "4",
                     "pid": 3,
                     "children": []
                   }
                ]
            }
        ]
    }
]

function flat(tree) {
   return  tree.reduce((res, item) => {
        const {children, ...i} = item
        return res.concat(i, children && children.length ? flat(children) : [])
    }, [])
}

console.log(flat(tree));